/**
 * Copyright (c) 2020 kedacom
 * OpenATC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 * http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 **/
<template>
  <div class="phase-associated-component">
    <div class="phase-box" v-for="(item, index) in assophaseList" :key="index">
      <div class="single-phase" :class="preselectPhase == item.id ? 'single-phase-select' : ''">
        <!-- <div class="ped-icon">
          <div v-for="(side, index) in item.peddirection" :key="'peddir-' + index">
            <PatternWalkSvg :Data="getshowped(side)" Width="42" Height="44" />
          </div>
        </div> -->

        <div class="phase-icon" @click="selectPhase(index, item)" v-if="item.direction">
          <xdr-dir-selector Width="44px" Height="44px" :showlist="getShowlist(item.direction)" :roadDirection="roadDirection"></xdr-dir-selector>
        </div>
        <div class="delete-phase-icon" @click="handleDeletePhase(index, item)">
          <i class="el-icon-close"></i>
        </div>
      </div>
      <div class="single-phase-name">{{$t('edge.channelizationmap.phase') + item.id}}</div>
    </div>
    <div class="phase-box">
      <div class="single-phase add-phase" @click="handleAddPhase()">
        <i class="iconfont icon-fangda" :class="{'add-disabled': !isCanAdd}"></i>
      </div>
    </div>
  </div>
</template>
<script>
import { mapState } from 'vuex'
import PhaseDataModel from 'openatc-components/package/kisscomps/components/IntersectionMap/crossDirection/utils.js'
import PatternWalkSvg from 'openatc-components/package/kisscomps/components/IntersectionMap/crossDirection/baseImg/PatternWalkSvg'
export default {
  name: 'phase-associated-component',
  data () {
    return {
      assophaseList: [], // 渠化界面的相位列表
      preselectPhase: -1,
      id: 1,
      isCanAdd: true
    }
  },
  components: {
    PatternWalkSvg
  },
  props: {
    editData: {
      type: Object
    },
    Motorways: {
      type: Array
    }
  },
  computed: {
    ...mapState({
      associatedphaselist: state => state.globalParam.associatedPhaseList,
      roadDirection: state => state.globalParam.roadDirection,
      PhaseDataMgr: state => state.globalParam.PhaseDataMgr
    })
  },
  watch: {
    associatedphaselist: {
      handler: function (list) {
        this.assophaseList = JSON.parse(JSON.stringify(list))
      },
      deep: true
    },
    assophaseList: {
      handler: function (list) {
        if (list.length >= 40) {
          this.isCanAdd = false
        } else {
          this.isCanAdd = true
        }
      },
      deep: true
    },
    editData: {
      handler: function (editobj) {
        if (editobj.phaseid !== undefined) {
          this.preselectPhase = editobj.phaseid
        } else {
          this.preselectPhase = -1
        }
      },
      deep: true
    }
  },
  methods: {
    selectPhase (index, item) {
      let direction = this.PhaseDataMgr.editDirPosToPhaseDireaciton(this.editData.icondireid, this.editData.lanePosition)
      // 校验同方位车道不能关联不同相位
      if (this.PhaseDataMgr.checkSameDirectionPhase(direction, item.id, this.editData.phaseid, this.Motorways, this.editData) === false) {
        this.$message.error(this.$t('edge.channelizationmap.associatedphaseerror'))
        return
      }
      // 关联相位
      this.preselectPhase = item.id
      let alldirection = direction
      this.$emit('selectPhase', this.preselectPhase, alldirection)
    },
    changeDirection (choosedicon) {
      for (let i = 0; i < this.assophaseList.length; i++) {
        if (this.assophaseList[i].id === choosedicon.phaseid) {
          let direction = this.PhaseDataMgr.editDirPosToPhaseDireaciton(choosedicon.icondireid, choosedicon.lanePosition, this.preselectPhase)
          // 校验同方位车道不能关联不同相位
          if (this.PhaseDataMgr.checkSameDirectionPhase(direction, choosedicon.phaseid) === false) {
            this.$message.error(this.$t('edge.channelizationmap.associatedphaseerror'))
            return
          }
          let alldirection = direction
          this.$emit('selectPhase', this.preselectPhase, alldirection)
        }
      }
    },
    getShowlist (direction) {
      return direction.map(dir => {
        return {
          id: dir,
          color: '#606266'
        }
      })
    },
    getshowped (peddirection) {
      return {
        id: peddirection,
        name: this.PhaseDataModel.getSidePos(peddirection).name
      }
    },
    handleDeletePhase (index, row) {
      // 更新模型内初始相位列表数据
      this.PhaseDataMgr.deleteOriginPhase(index)
      // 删除关联此相位的所有车道相位id
      this.handleDisassociatePhase(row.id)
      if (row.id === this.preselectPhase) {
        this.preselectPhase = -1
      }
    },
    handleDisassociatePhase (deletePhaseid) {
      // 删除相位后，解除相位关联
      this.$emit('handleDisassociatePhase', deletePhaseid)
    },
    handleAddPhase () {
      if (this.assophaseList.length >= 40) {
        this.$message.error(
          this.$t('edge.phase.mostdata')
        )
        return
      }
      // 更新模型内初始相位列表数据
      this.PhaseDataMgr.addOriginPhase()
    },
    getPedPhasePos () {
    // 行人相位信息
      this.sidewalkPhaseData = []
      this.assophaseList.forEach((ele, i) => {
        if (ele.peddirection) {
          ele.peddirection.forEach((dir, index) => {
            // 行人相位
            if (this.PhaseDataModel.getSidePos(dir)) {
              this.sidewalkPhaseData.push({
                key: this.CrossDiagramMgr.getUniqueKey('pedphase'),
                phaseid: ele.id, // 相位id，用于对应相位状态
                id: dir,
                name: this.PhaseDataModel.getSidePos(dir).name
              })
            }
          })
        }
      })
      return this.sidewalkPhaseData
    }
  },
  created () {
    this.globalParamModel = this.$store.getters.globalParamModel
    this.PhaseDataModel = new PhaseDataModel()
  },
  mounted () {
    this.assophaseList = JSON.parse(JSON.stringify(this.associatedphaselist))
  },
  destroyed () {
  }
}
</script>
<style lang="scss">
</style>
